// const sayHello = (name: string): string => `Hello, ${name}`

let fn = () => {}
//let fun: () => void //TS 根据fn，自动进行类型推断的结果

// fn = '101' // 编译报错，不能将类型“string”分配给类型“() => void”。

let fn2 = () => {}

let fn3 = '101' as unknown as string
console.log(fn3) //=> 101

console.log('===')

const sum = (a: number, b: number): number => a + b

let res: number = sum(1, 2)
console.log(res) //=> 3

const clg = (): void => console.log('不指定返回值的函数')

// 没有指定返回值，默认就返回 undefined
console.log(clg()) //=> '不指定返回值的函数' undefined

function sum2(a: number, b: number, ratio?: number) {
  return a + b
}

console.log(sum2(3, 3)) //=> 6

const printId = (id: number): number => id
const printId2 = (id: number) => id //=> 类型推断结果 const printId2: (id: number) => number

console.log('===')

// 匿名函数

console.log('===')

// 有多个函数会用到相同的类型的参数，我们可以使用 type / interface 对参数对象进行声明
type typeUser = {
  name: string
  age: number
}

const addUser = (user: typeUser): void => console.log('type 添加用户')
const updateUser = (user: typeUser): void => console.log('type 更新用户')
addUser({ name: '喵酱', age: 1 }) //=> 'type 添加用户'
updateUser({ name: '喵酱', age: 2 }) //=> 'type 更新用户'

interface IUser {
  name: string
  age: number
}

const addUser2 = (user: IUser): void => console.log('interface 添加用户')
const updateUser2 = (user: IUser): void => console.log('interface 更新用户')
addUser2({ name: '喵酱', age: 1 }) //=> 'interface 添加用户'
updateUser2({ name: '喵酱', age: 2 }) //=> 'interface 更新用户'

console.log('===')

export {}
